<include file='Index/header' />
<div class="row">
	<br>
	<div class="col-sm-12">
		<div class="col-sm-2" style="padding-top:5px">
			<label for="return_serial_id" class="control-label">退货单流水号：</label>
		</div>
		<div class="col-sm-2">
			<input id="return_serial_id" class="form-control"></input>
		</div>
		<div class="col-sm-2" style="padding-left:70px;padding-top:5px">
			<label for="order_serial_id" class="control-label">订单号：</label>
		</div>
		<div class="col-sm-2">
			<input id="order_serial_id" class="form-control"></input>
		</div>
		<div class="col-sm-2" style="padding-left:70px;padding-top:5px">
			<label for="receivename" class="control-label">收货人：</label>
		</div>
		<div class="col-sm-2">
      <input id="receivename" class="form-control"></input>
    </div>
	</div>
	<div class="col-sm-12">
		<br>
	<table id="order_list_table" class="table table-hover table-bordered table-striped table-striped">
		<thead>
			<tr>
				<td>退货单流水号</td>
				<td>发货单流水号</td>
				<td>订单号</td>
				<td>下单时间</td>
				<td>收货人</td>
				<td>发货时间</td>
				<td>退货时间</td>
				<td>总金额</td>
				<td>发货仓库</td>
				<td>应付金额</td>
				<td>操作人</td>
				<td>操作</td>
			</tr>
		</thead>
		<tbody>

		</tbody>
	</table>
</div>
</div>

<include file='Index/footer' />
<include file='Order/return-detail' />
<script>
$(document).ready(function() {
		loadTable();
});
var table;
function loadTable(){
	table = $('#order_list_table').DataTable({
		"bProcessing": true,
		"bDestroy": true,
		"order": [[ 6, "desc" ]],
		"sPaginationType" : "input",
		"oLanguage" : {
              "sLengthMenu": "每页显示 _MENU_ 条记录",
              "sZeroRecords": "抱歉， 没有找到",
              "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
              "sInfoEmpty": "没有数据",
              "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
              "sZeroRecords": "没有检索到数据",
               "sSearch": "搜索: &nbsp",
              "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
              }
          }, 
		"ajax":'{:U("Admin/Order/getReturn/")}',
		"columns": [
					{ "data": 'return_serial_id' },
					{ "data": 'deliver_serial_id' },
					{ "data": 'order_serial_id' },
					{ "data": 'createtime' },
	        { "data": 'receivename' },
	        { "data": 'createtime' },
	        { "data": 'returntime' },
	        { "data": 'price' },
	        { "data": 'repository' },
	        { "data": 'realprice' },
	        { "data": 'admin_name' },
	        { "data": 'btn' }
    ],
    "columnDefs": [
    {
			"targets": [1, 5],
			"visible":false
    },
    {
			"targets": 4,
			"render": function (data, type, row, table) {
        return row.receivename + '<br>' + row.phone + '<br>' + row.address;
      }
    },
    {
			"targets": [7, 9],
			"render": function (data, type, row, table) {
        return (parseFloat(data) + parseFloat(row.sendprice)).toFixed(2) + '元';
      }
    },
    {
			"targets": 8,
			"render": function (data, type, row, table) {
        return (row.a_repository_name?row.a_repository_name:"") + (row.b_repository_name?row.b_repository_name:"");
      }
    },
    {
    	"targets": -1,
    	"data": null,
    	"render": function (data, type, row, table) {
        return '<button class="btn btn-info" onclick="view(\''+row.order_id+'\')">查看</button>';
      },
    }]
	});

	$("#return_serial_id").keyup(function(){
		table.column(0).search(this.value).draw();
  });

	$("#order_serial_id").keyup(function(){
		table.column(2).search(this.value).draw();
  });

  $("#receivename").keyup(function(){
		table.column(4).search(this.value).draw();
  });
}

function view(order_id){
	$.ajax({
			url: '{:U("Admin/Order/getReturn/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"order_id":order_id
			}
		})
		.success(function(data){
			switch(data.status){
				case '0':
					$('#status').html('已发货');
					break;
				case '1':
					$('#status').html('已付款');
					break;
				case '2':
					$('#status').html('已完成');
					break;
				case '3':
					$('#status').html('待退货');
					break;
				case '4':
					$('#status').html('已退货');
					break;
			}

			$('#returntime').html(data.returntime);
			$('#order_serial_id_show').html(data.order_serial_id);
			$('#createtime1').html(data.createtime);
			$('#createtime2').html(data.createtime);
			$('#username').html(data.user.name);
			$('#price').html((parseFloat(data.price)+parseFloat(data.sendprice)).toFixed(2) + "元");
			$('#realprice').html((parseFloat(data.realprice)+parseFloat(data.sendprice)).toFixed(2) + "元");
			$('#receivename_show').html(data.receivename);
			$('#address').html(data.address);
			$('#phone').html(data.phone);

			var sendTime = 39;
			var products = data.products;
			var product_html = '';
			var product_amount = 0; 
			var product_realprice = 0;
			for (var i = 0; i < products.length; i++) {
				//如果商品sendtype!=0，选择新的sentime
				if(products[i].b_repository_id == null){
					if(products[i].sendtype != '0'){
						if(products[i].sendtype == '1'){
							sendTime = Math.max(sendTime, 49);
						}else if(products[i].sendtype == '2'){
							sendTime = Math.max(sendTime, 59);
						}
					}
				}else if(products[i].a_repository_id == null){
					sendTime = 1*24*60;
				}

				if(products[i].status == '1'){
					product_html += '<tr>'+
                			'<td>'+transNull(products[i].name)+'</td>'+
                			'<td>'+transNull(products[i].barcode)+'</td>'+
                			'<td>'+transNull(products[i].kj_price)+'</td>'+
                			'<td>'+transNull(products[i].num)+'</td>'+
                			'<td>'+transNull(products[i].origin_place)+'</td>'+
                			'<td>'+transNull(products[i].default_amount)+'</td>'+
                			'<td>'+(products[i].kj_price*products[i].num).toFixed(2)+'</td></tr>';
          product_amount += products[i].kj_price*products[i].num;
          product_realprice += products[i].realprice*products[i].num;  
        }   			
			}

			//收货时间的计算
			if(data.type == '0'){
				$('#receivetime').html(addDate(data.createtime, sendTime));
			}else{
				$('#receivetime').html(addDate(data.createtime, 1*24*60));
			}

			product_html += '<tr><td></td><td></td><td></td><td></td><td></td><td>合计</td><td>'+product_amount.toFixed(2)+'</td></tr>';
			$('#product').html(product_html);
			$('#product_price').html("￥"+product_amount.toFixed(2)+" - 折扣：￥"+(product_amount-product_realprice).toFixed(2)+" +  配送费：￥"+parseFloat(data.sendprice).toFixed(2));
			$('#product_realprice').html("￥"+(parseFloat(product_realprice)+parseFloat(data.sendprice)).toFixed(2));

			var log = data.log;
			var admin = data.admin;
			var log_html = '';
			for (var i = 0; i < log.length; i++) {
				var orderstatus, paystatus = '已付款', returnstatus = '已发货';
				switch(log[i].status){
					case '0':
						orderstatus = '已发货';
						paystatus = '未付款';
						break;
					case '1':
						orderstatus = '已付款';
						break;
					case '2':
						orderstatus = '已完成';
						break;
					case '3':
						orderstatus = '待退货';
						break;
					case '4':
						orderstatus = '已退货';
						break;
				}
				log_html += '<tr>'+
					'<td>'+transNull(admin[i].name)+'</td>'+
					'<td>'+transNull(log[i].createtime)+'</td>'+
					'<td>'+transNull(orderstatus)+'</td>'+
					'<td>'+transNull(paystatus)+'</td>'+
					'<td>'+transNull(returnstatus)+'</td>'+
					'<td>'+transNull(log[i].note)+'</td>'+
					'</tr>';
			}
			
			$('#log_table').html(log_html);

			$('#return_detail_modal').modal({
				backdrop : "static"
			});
	});
}
</script>
